package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.*;
import com.guochenglang.www.service.*;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.view.vo.CommentView;
import com.guochenglang.www.view.vo.DialogView;
import com.guochenglang.www.view.vo.LikeView;
import com.guochenglang.www.view.vo.ReplyView;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * 消息提醒Controller层
 *
 * @author GuoChenglang
 */

public class MessageController {
    private static final MessageService MESSAGE_SERVICE = MyDependency.returnObject(MessageService.class);
    private static final LikesService LIKES_SERVICE = MyDependency.returnObject(LikesService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);
    private static final ArticleService ARTICLE_SERVICE = MyDependency.returnObject(ArticleService.class);
    private static final CommentService COMMENT_SERVICE = MyDependency.returnObject(CommentService.class);
    private static final ReplyService REPLY_SERVICE = MyDependency.returnObject(ReplyService.class);
    private static final DialogService DIALOG_SERVICE = MyDependency.returnObject(DialogService.class);

    /**
     * 获得点赞消息的个数
     */
    public static Result getLikeCount(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        int num = MESSAGE_SERVICE.likeMessageCount(userId);
        return Result.result(num);
    }

    /**
     * 获得点赞消息
     */
    public static Result getLikeMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得点赞消息列表
        ArrayList<LikeMessage> likeMessages = MESSAGE_SERVICE.showLikeMessage(userId);
        ArrayList<Likes> likes = new ArrayList<>();
        ArrayList<LikeView> likeViews = new ArrayList<>();
        for (LikeMessage likeMessage : likeMessages) {
            LikeView likeView = new LikeView();
            likeView.setMessageId(likeMessage.getId());
            //获得点赞
            likes.add(LIKES_SERVICE.getLike(likeMessage.getLikeId()));
            //封装进结果
            likeViews.add(likeView);
        }
        ArrayList<User> likeUsers = new ArrayList<>();
        ArrayList<Article> likeArticles = new ArrayList<>();
        int index = 0;
        //根据id获得用户以及被点赞的文章，返回前端显示
        for (Likes like : likes) {
            User user = USER_SERVICE.getUser(like.getLikeId());
            Article article = ARTICLE_SERVICE.getArticle(like.getArticleId());
            likeViews.get(index).setUserId(user.getId());
            likeViews.get(index).setArticle(article);
            likeViews.get(index).setUsername(user.getUsername());
            index++;
        }
        return Result.result(likeViews);
    }

    /**
     * 删除点赞消息
     */
    public static Result deleteLikeMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, IOException {
        //获得消息id
        Integer messageId = GetObject.getObject(request, Integer.class);
        int num = MESSAGE_SERVICE.deleteLikeMessage(messageId);
        return Result.result(num);
    }

    /**
     * 获得评论消息的个数
     */
    public static Result getCommentCount(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        int num = MESSAGE_SERVICE.commentCount(userId);
        return Result.result(num);
    }

    /**
     * 获得评论信息
     */
    public static Result getCommentMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得评论消息列表
        ArrayList<CommentMessage> commentMessages = MESSAGE_SERVICE.showCommentMessage(userId);
        ArrayList<Comment> comments = new ArrayList<>();
        ArrayList<CommentView> commentViews = new ArrayList<>();
        //获得具体评论
        for (CommentMessage commentMessage : commentMessages) {
            CommentView commentView = new CommentView();
            commentView.setMessageId(commentMessage.getId());
            Comment condition = new Comment();
            condition.setId(commentMessage.getCommentId());
            //通过id获得所有评论
            comments.add(COMMENT_SERVICE.getComment(condition).get(0));
            //结果封装
            commentViews.add(commentView);
        }
        //获得用户并且对应文章，返回前端显示
        int index = 0;
        for (Comment comment : comments) {
            User user = USER_SERVICE.getUser(comment.getCommentId());
            Article article = ARTICLE_SERVICE.getArticle(comment.getArticleId());
            //封装结果
            commentViews.get(index).setArticle(article);
            commentViews.get(index).setUsername(user.getUsername());
            commentViews.get(index).setUserId(user.getId());
            index++;
        }
        System.out.println("长度是：" + commentViews.size());
        return Result.result(commentViews);
    }

    /**
     * 删除评论消息
     */
    public static Result deleteCommentMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, IOException {
        //获得用户id
        Integer userId = GetObject.getObject(request, Integer.class);
        int num = MESSAGE_SERVICE.deleteCommentMessage(userId);
        return Result.result(num);
    }

    /**
     * 获得回复消息的数目
     */
    public static Result getReplyCount(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得个数
        int num = MESSAGE_SERVICE.replyCount(userId);
        return Result.result(num);
    }

    /**
     * 获得回复消息信息
     */
    public static Result getReplyMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得回复消息
        ArrayList<ReplyMessage> replyMessages = MESSAGE_SERVICE.showReplyMessage(userId);
        ArrayList<Reply> replies = new ArrayList<>();
        ArrayList<ReplyView> replyViews = new ArrayList<>();
        //获得具体回复信息
        for (ReplyMessage replyMessage : replyMessages) {
            Reply condition = new Reply();
            condition.setId(replyMessage.getReplyId());
            replies.add(REPLY_SERVICE.getReply(condition).get(0));
            ReplyView replyView = new ReplyView();
            replyView.setMessageId(replyMessage.getId());
            replyViews.add(replyView);
        }
        //根据id获得具体回复人以及对应文章并返回前端显示
        int index = 0;
        for (Reply reply : replies) {
            User user = USER_SERVICE.getUser(reply.getUserId());
            Comment condition = new Comment();
            condition.setId(reply.getCommentId());
            Integer articleId = COMMENT_SERVICE.getComment(condition).get(0).getArticleId();
            Article article = ARTICLE_SERVICE.getArticle(articleId);
            replyViews.get(index).setUsername(user.getUsername());
            replyViews.get(index).setUserId(user.getId());
            replyViews.get(index).setArticle(article);
            index++;
        }
        return Result.result(replyViews);
    }

    /**
     * 删除回复消息
     */
    public static Result deleteReplyMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, IOException {
        //获得消息的id
        Integer messageId = GetObject.getObject(request, Integer.class);
        int num = MESSAGE_SERVICE.deleteReplyMessage(messageId);
        return Result.result(num);
    }

    /**
     * 获得未读聊天信息
     */
    public static Result getDialogMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        //获得未读会话列表
        ArrayList<Dialog> dialogs = DIALOG_SERVICE.dialogNoRead(userId);
        HashMap<User, Integer> users = new HashMap<>();
        ArrayList<DialogView> dialogViews = new ArrayList<>();
        //根据不同的用户获取不同的未读会话个数
        for (Dialog dialog : dialogs) {
            //封装vo
            DialogView dialogView = new DialogView();
            dialogView.setDialog(dialog);
            User user = USER_SERVICE.getUser(dialog.getUserId());
            if (!users.containsKey(user)) {
                users.put(user, 1);
            } else {
                int count = users.get(user);
                users.put(user, count + 1);
            }
            dialogViews.add(dialogView);
        }
        Set<User> users1 = users.keySet();
        int index = 0;
        //将map数据封装进list集合中
        for (User user : users1) {
            Integer count = users.get(user);
            dialogViews.get(index).setUsername(user.getUsername());
            dialogViews.get(index).setDialogCount(count);
            index++;
        }
        return Result.result(dialogViews);
    }

    /**
     * 获得未读会话个数
     */
    public static Result getDialogCount(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        int num = DIALOG_SERVICE.dialogNoReadCount(userId);
        return Result.result(num);
    }
}
